<template>
  <div class="app-container">
    <el-card shadow="never">
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="医院名称">
          <el-input v-model="searchForm.hospitalAme" clearable>
          </el-input>
        </el-form-item>
        <el-form-item label="负责人">
          <el-input v-model="searchForm.headPeo" clearable>
          </el-input>
        </el-form-item>
        <el-form-item label="负责人电话">
          <el-input v-model="searchForm.headTel" clearable>
          </el-input>
        </el-form-item>
        <el-form-item label="医院等级">
          <el-select v-model="searchForm.hospitalGrade" placeholder="请选择级别" clearable>
            <el-option v-for="item in hospitalGradeList" :key="item" :label="item" :value="item">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleFilter" class="search-button">查询</el-button>
          <el-button type="primary" @click="showAdd" class="add-button">添加</el-button>
          <el-button @click="handleReset" class="search-button">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table v-loading="listLoading" :data="list" element-loading-text="Loading" fit highlight-current-row>
        <el-table-column align="center" type="index" label="序号" width="55">
        </el-table-column>
        <el-table-column label="医院名称" align="center" prop="hospitalAme">
        </el-table-column>
        <el-table-column label="医院等级" align="center" prop="hospitalGrade">
        </el-table-column>
        <el-table-column label="负责人" align="center" prop="headPeo">
        </el-table-column>
        <el-table-column label="负责人电话" align="center" prop="headTel">
        </el-table-column>
        <el-table-column label="缩略图" align="center" prop="fileName">
          <template slot-scope="{row}">
            <el-image style="width: 100px; height: 100px" :src="row.fileName" :preview-src-list="[row.fileName]">
            </el-image>
          </template>
        </el-table-column>
        <el-table-column label="详细地址" align="center" prop="detailAddress">
        </el-table-column>
        <el-table-column align="center" label="操作" width="200">
          <template slot-scope="{row}">
            <el-button @click="showDetails(row)" type="text">查看</el-button>
            <el-button @click="showEdit(row)" type="text">编辑</el-button>
            <el-button @click="delHandle(row)" type="text">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total > 0" :total="total" :page.sync="searchForm.pageIndex" :limit.sync="searchForm.pageSize"
        @pagination="getList" />
    </el-card>
    <el-dialog :title="dialogTit" :visible.sync="dialogVisible" width="80%">
      <el-form label-position="right" label-width="120px" ref="form" :model="form">
        <el-divider content-position="left"><i style="margin-right: 20px;" class="el-icon-user"></i>医院信息</el-divider>
        <el-form-item label="医院名称">
          <el-input v-model.trim="form.hospitalAme"></el-input>
        </el-form-item>
        <el-form-item label="医院等级">
          <el-select v-model="form.hospitalGrade" placeholder="请选择">
            <el-option v-for="item in hospitalGradeList" :key="item" :label="item" :value="item">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="负责人电话">
          <el-input v-model.trim="form.headTel"></el-input>
        </el-form-item>
        <el-form-item label="负责人">
          <el-input v-model.trim="form.headPeo"></el-input>
        </el-form-item>
        <el-form-item label="缩略图">
          <uploadImg v-if="dialogVisible" v-model="form.fileName"></uploadImg>
        </el-form-item>
        <el-form-item label="医院简介">
          <el-input v-model.trim="form.profile" type="textarea" :rows="4"></el-input>
        </el-form-item>
        <el-form-item label="详细地址">
          <el-input v-model.trim="form.detailAddress"></el-input>
        </el-form-item>
        <el-divider content-position="left"><i style="margin-right: 20px;" class="el-icon-user"></i>医院管理员账号</el-divider>
        <el-form-item label="账户名称">
          <el-input v-model.trim="form.amountName"></el-input>
        </el-form-item>
        <el-form-item label="登陆密码">
          <el-input v-model.trim="form.pwd"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" class="btn-cancel">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog title="详情" :visible.sync="detailsDialog" width="80%">
      <el-form label-position="right" label-width="120px" ref="form" :model="form">
        <el-divider content-position="left"><i style="margin-right: 20px;" class="el-icon-user"></i>医院信息</el-divider>
        <el-form-item label="医院名称">
          <div>{{ form.hospitalAme }}</div>
        </el-form-item>
        <el-form-item label="医院等级">
          <div>{{ form.hospitalGrade }}</div>
        </el-form-item>
        <el-form-item label="负责人电话">
          <div>{{ form.headTel }}</div>
        </el-form-item>
        <el-form-item label="负责人">
          <div>{{ form.headPeo }}</div>
        </el-form-item>
        <el-form-item label="缩略图">
          <el-image style="width: 100px; height: 100px" :src="form.fileName" :preview-src-list="[form.fileName]">
          </el-image>
        </el-form-item>
        <el-form-item label="医院简介">
          <div>{{ form.profile }}</div>
        </el-form-item>
        <el-form-item label="详细地址">
          <div>{{ form.detailAddress }}</div>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="detailsDialog = false" class="btn-cancel">关闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import * as HospitalManagement from '@/api/HospitalManagement'
import Pagination from '@/components/Pagination'
import uploadImg from '../../components/uploadImg.vue'
export default {
  components: { Pagination, uploadImg },
  data () {
    return {
      list: null,
      listLoading: true,
      searchForm: {
        pageIndex: 1,
        pageSize: 10
      },
      total: 0,
      dialogVisible: false,
      detailsDialog: false,
      dialogTit: '',
      form: {},
      hospitalGradeList: [
        '一级甲等',
        '一级乙等',
        '一级丙等',
        '二级甲等',
        '二级乙等',
        '二级丙等',
        '三级甲等',
        '三级乙等',
        '三级丙等',
      ]
    }
  },
  created () {
    this.getList()
  },
  methods: {
    getList () {
      this.listLoading = true
      let param = Object.assign({}, this.searchForm)
      HospitalManagement.queryByPage(param).then(response => {
        this.list = response.data.rows
        this.total = response.data.total
        this.listLoading = false
      }).catch(error => {
        this.listLoading = false
        console.log(error)
      })

    },
    handleFilter () {
      this.searchForm.pageIndex = 1
      this.getList()
    },
    handleReset () {
      this.resetTemp();
      this.getList();
    },
    resetTemp () {
      this.searchForm = {
        pageIndex: 1,
        pageSize: 10
      }
    },
    showAdd () {
      this.dialogTit = '新增'
      this.form = {}
      this.dialogVisible = true
    },
    showEdit (row) {
      this.dialogTit = '编辑'
      this.form = Object.assign({}, row)
      delete this.form.createTime
      delete this.form.updateTime
      this.dialogVisible = true
    },
    showDetails (row) {
      this.form = Object.assign({}, row)
      this.detailsDialog = true
    },
    submit () {
      if (this.dialogTit === '新增') {
        this.creatHandle()
      } else if (this.dialogTit === '编辑') {
        this.editHandle()
      }
    },
    creatHandle () {
      HospitalManagement.insert(this.form).then(response => {
        if (response.code == 200) {
          this.dialogVisible = false
          this.getList()
          this.$message.success('新增成功')
        }
      }).catch(error => {
        console.log(error)
      })
    },
    editHandle () {
      HospitalManagement.update(this.form).then(response => {
        if (response.code == 200) {
          this.dialogVisible = false
          this.getList()
          this.$message.success('修改成功')
        }
      }).catch(error => {
        console.log(error)
      })
    },
    delHandle (data) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let param = {
          uid: data.uid,
        }
        HospitalManagement.deleteById(param).then(result => {
          this.$message.success(result.msg)
          this.getList()
        }).catch(error => {
          this.loading = false
          console.log(error)
        })
      }).catch(() => {
        console.log('取消删除')
      });
    }
  }
}
</script>
